/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.typhosoft.jcommon.observer;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author SantonocitoM
 */
public abstract class AServer implements IServer {

    protected final List<IClient> fConsumerList = new ArrayList();
    private static final NotifierExecutor notifierExecutor = new NotifierExecutor();

    protected AServer() {
    }

    @Override
    public boolean addObserver(IClient consumer) {
        boolean b = false;
        synchronized (fConsumerList) {
            if (!fConsumerList.contains(consumer)) {
                b = fConsumerList.add(consumer);
            }

        }

        return b;
    }

    @Override
    public boolean removeObserver(IClient consumer) {
        boolean b = false;
        synchronized (fConsumerList) {
            if (fConsumerList.contains(consumer)) {
                b = fConsumerList.remove(consumer);
            }
        }
        return b;
    }

    @Override
    public void notifyObservers(List notifications) {
        synchronized (fConsumerList) {
            Iterator<IClient> consumers = fConsumerList.iterator();

            while (consumers.hasNext()) {
                IClient client = consumers.next();
                notifyObserver(client, notifications);
            }
        }
    }

    protected void notifyObserver(IClient consumer, List notifications) {
        notifierExecutor.notifyObserver(consumer, notifications);
    }
}
